package cn.wolfcode.car.business.web.controller;


import cn.wolfcode.car.base.domain.User;
import cn.wolfcode.car.base.service.IUserService;
import cn.wolfcode.car.business.domain.Appointment;
import cn.wolfcode.car.business.domain.ServiceItem;
import cn.wolfcode.car.business.domain.Statement;
import cn.wolfcode.car.business.domain.StatementItem;
import cn.wolfcode.car.business.query.ServiceItemQuery;
import cn.wolfcode.car.business.query.StatementItemQuery;
import cn.wolfcode.car.business.service.IServiceItemService;
import cn.wolfcode.car.business.service.IStatementItemService;
import cn.wolfcode.car.business.service.IStatementService;
import cn.wolfcode.car.common.base.page.TablePageInfo;
import cn.wolfcode.car.common.config.SystemConfig;
import cn.wolfcode.car.common.web.AjaxResult;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.math.BigDecimal;
import java.util.List;

/**
 * 客户控制器
 */
@Controller
@RequestMapping("/business/statementItem")
public class StatementItemController {
    //模板前缀
    private static final String prefix = "/business/statementItem/";

    //明细表对象
    @Autowired
    private IStatementItemService statementItemService;

    //结算表对象
    @Autowired
    private IStatementService statementService;

    @Autowired
    private IUserService userService;


    //页面------------------------------------------------------------
    //列表
    @RequiresPermissions("business:statementItem:view")
    @RequestMapping("/listPage")
    public String listPage(){
        return prefix + "list";
    }



    @RequiresPermissions("business:statementItem:edit")
    @RequestMapping("/editPage")
    public String editPage(Long id, Model model){
        model.addAttribute("statementItem", statementItemService.get(id));
        return prefix + "edit";
    }

    //数据-----------------------------------------------------------
    //列表
    @RequiresPermissions("business:statementItem:list")
    @RequestMapping("/query")
    @ResponseBody
    public TablePageInfo<StatementItem> query(StatementItemQuery qo){
        return statementItemService.query(qo);
    }




    //新增
    @RequiresPermissions("business:statementItem:add")
    @RequestMapping("/add")
    @ResponseBody
    public AjaxResult addSave(StatementItem statementItem){
        statementItemService.save(statementItem);
        return AjaxResult.success();
    }




    @RequiresPermissions("business:statementItem:itemDetail")
    @RequestMapping("/itemDetail")
    public String itemDetail(Long statementId,Model model){

        //根据结算单中当前行的状态跳转不同的界面
        Statement statement = statementService.get(statementId);
        model.addAttribute("statement",statement);
        //判断当前行的状态
        if (Statement.STATUS_CONSUME.equals(statement.getStatus())) {
            //消费状态 //消费中 -- editDetail
            return prefix + "editDetail";

        }else {

            //查询收款人信息,根据结算单对象中的收款人id，查询收款人信息
            User user = userService.get(statement.getPayeeId());
            //塞进statement对象中
            statement.setPayee(user);

            //已结算中 -- showDetail.html
            return prefix + "showDetail";
        }
    }

    //处理保存按钮的操作
    @RequiresPermissions("business:statementItem:saveItems")
    @RequestMapping("/saveItems")
    @ResponseBody
    public AjaxResult saveItems(@RequestBody List<StatementItem> items) {
        statementItemService.saveItems(items);
        return  AjaxResult.success();
    }


    //支付操作
    @RequiresPermissions("business:statementItem:payStatement")
    @RequestMapping("/payStatement")
    @ResponseBody
    public AjaxResult payStatement(Long statementId) {

        //修改结算单中的数据，需要使用结算单对象
        statementService.updateByStatementId(statementId);
        SystemConfig.getProfile();
        return  AjaxResult.success();
    }

}
